package com.taotao.service.impl;

import com.taotao.common.enums.OperateType;
import com.taotao.common.pojo.BaseResult;
import com.taotao.common.utils.Constants;
import com.taotao.common.utils.DateUtil;
import com.taotao.mapper.EyMemberMapper;
import com.taotao.mapper.EyTransferMapper;
import com.taotao.pojo.EyMember;
import com.taotao.pojo.EyTransfer;
import com.taotao.service.OperateLogService;
import com.taotao.service.TransferService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class TransferServiceImpl implements TransferService {
    @Autowired
    EyMemberMapper eyMemberMapper;
    @Autowired
    EyTransferMapper eyTransferMapper;
    @Autowired
    OperateLogService operateLogService;

    BaseResult baseResult = new BaseResult();

    @Override
    public Map<String, Object> transferAdd(Map<String, String> map) {
        try {
            String sender = map.get("sender");
            String receiver=map.get("receiver");
            String serviceId = map.get("serviceId");
            String goodsId = map.get("goodsId");
            String amount = map.get("amount");
            String remark = map.get("remark");
            String status = map.get("status");

            EyTransfer transfer = new EyTransfer();
            transfer.setSender(Integer.parseInt(sender));
            transfer.setReceiver(Integer.parseInt(receiver));
            Integer sid=null;
            Integer gid=null;
            if (StringUtils.isNotBlank(serviceId)) {
                sid=Integer.parseInt(serviceId);
                transfer.setServiceId(sid);
            }
            if (StringUtils.isNotBlank(goodsId)) {
                gid=Integer.parseInt(goodsId);
                transfer.setGoodsId(gid);
            }
            transfer.setAmount(amount);
            transfer.setRemark(remark);
            transfer.setStatus(status); //1待付款 2已付款
            transfer.setCreateTime(DateUtil.getDateAndTime());
            transfer.setUpdateTime(DateUtil.getDateAndTime());
            eyTransferMapper.insert(transfer);

            Map<String, Object> m = new HashMap<>();
            m.put("transferId", transfer.getId());
            operateLogService.insert(Integer.parseInt(sender), sid, gid, OperateType.operateType_45.getDesc(), OperateType.operateType_45.getCode(), "");

            return baseResult.getSuccMap(m);

        } catch (Exception e) {
            e.printStackTrace();
            return baseResult.getErrorMap("网络繁忙，请稍后再试");
        }
    }

}
